package com.example.dell.uitest;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView flagImageView;
    private TextView flagTxt;

    private ImageButton backImageBtn;
    private ImageButton forwardImageBtn;

    private int[] flag={R.drawable.ytu_200,R.drawable.pku_200,R.drawable.tsinghua_200};
    private String[] universityNames={"烟台大学","北京大学","清华大学"};

    private int currentPage=0;

    private Button chooseMyUniBtn;
    private TextView chooseMyUniTxt;

    private String myUniversity;

    private Button likeUniBtn;
    private TextView likeUniTxt;

    private boolean[] likeUniversitys={false,false,false};

    private Switch logoSizeSwitch;
    private ImageView universityImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        flagImageView=(ImageView)findViewById(R.id.universityImageView);
        flagTxt=(TextView)findViewById(R.id.universityTxt);

        backImageBtn=(ImageButton)findViewById(R.id.backImageBtn);
        forwardImageBtn=(ImageButton)findViewById(R.id.forwardImageBtn);
        backImageBtn.setOnClickListener(onClickListener);
        forwardImageBtn.setOnClickListener(onClickListener);

        chooseMyUniBtn=(Button)findViewById(R.id.chooseUniBtn);
        chooseMyUniTxt=(TextView)findViewById(R.id.chooseUniTxt);
        chooseMyUniBtn.setOnClickListener(onClickListener);

        likeUniBtn=(Button)findViewById(R.id.likeUniBtn);
        likeUniTxt=(TextView) findViewById(R.id.likeUniTxt);
        likeUniBtn.setOnClickListener(onClickListener);

        //logoSizeSwitch=(Switch)findViewById(R.id.logoSizeSwitch);
        //universityImageView=(ImageView)findViewById(R.id.universityImageView);
        //logoSizeSwitch.setOnCheckedChangeListener(onCompoundButtonOnCheckedChange);
    }
    private View.OnClickListener onClickListener=new View.OnClickListener(){
        public void onClick(View v){
            switch(v.getId()){
                case R.id.backImageBtn:
                    if(currentPage==0){
                        Toast.makeText(MainActivity.this,"第一页，前面没有了",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    currentPage--;
                    flagImageView.setImageResource(flag[currentPage]);
                    flagTxt.setText(universityNames[currentPage]);
                    break;
                case R.id.forwardImageBtn:
                    if(currentPage==(flag.length-1)){
                        Toast.makeText(MainActivity.this,"最后一页，后面没有了",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    currentPage++;
                    flagImageView.setImageResource(flag[currentPage]);
                    flagTxt.setText(universityNames[currentPage]);
                    break;
                case R.id.chooseUniBtn:
                    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                    myUniversity="";
                    builder.setTitle("选择我的大学")
                            .setIcon(R.drawable.university)
                            .setSingleChoiceItems(universityNames,-1,new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog, int which){
                                    myUniversity=universityNames[which].toString();
                                }
                            })
                            .setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int which){
                                    if(myUniversity==""){
                                        chooseMyUniTxt.setText("选了个寂寞");
                                    }else{
                                        chooseMyUniTxt.setText(myUniversity);
                                        dialog.dismiss();
                                    }
                                }
                            })
                            .setNegativeButton("取消",new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int which){
                                chooseMyUniTxt.setText("放弃了选择");
                                dialog.dismiss();
                                }
                            })
                            .create().show();
                    break;
                case R.id.likeUniBtn:
                    AlertDialog.Builder builder2=new AlertDialog.Builder(MainActivity.this);
                    builder2.setTitle("选择我喜欢的大学")
                            .setIcon(R.drawable.university)
                            .setMultiChoiceItems(universityNames,likeUniversitys,new DialogInterface.OnMultiChoiceClickListener(){
                                public void onClick(DialogInterface dialogInterface,int i,boolean b){likeUniversitys[i]=b;
                                }
                            })
                            .setPositiveButton("确认",new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int which){
                                    String univesitysChoosed="";
                                    for(int i=0;i<likeUniversitys.length;i++){
                                        if(likeUniversitys[i]==true){
                                            univesitysChoosed+=(universityNames[i]+"");
                                        }
                                    }
                                    if(univesitysChoosed=="")
                                        univesitysChoosed="静待你有所喜欢";
                                    likeUniTxt.setText(univesitysChoosed);
                               }
                            })
                            .create()
                            .show();
                    break;
                default:
                    break;
                }
        }
            };
}
